package com.yxq.mdt.app.modules.personal.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.personal.bean.TopicCommentBean;
import com.yxq.mdt.app.modules.personal.model.PublishModel;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialStateDetailsAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.List;

/**
 * @author L
 * @time
 * @describe 我的回复
 */
public class MyReplyListAdapter extends BaseQuickAdapter<TopicCommentBean.ListBean, BaseViewHolder> {

    public MyReplyListAdapter(int layoutResId, List<TopicCommentBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, TopicCommentBean.ListBean item) {
        helper.addOnClickListener(R.id.personal_item_img);
        ImageView icon = helper.getView(R.id.personal_item_img);
        GlideHelper.setLoadRoundImage(mContext, item.getHeadimg(), R.mipmap.head_icon, icon);
        helper.setText(R.id.item_layout_nike_name, item.getGsName());
        String address = "";
        if (!TextUtils.isEmpty(item.getResidence()) && item.getResidence().split(";") != null && item.getResidence().split(";").length >= 2) {
            address = item.getResidence().split(";")[1];
        }
        helper.setText(R.id.item_layout_time, address);
        //性别
        helper.setImageResource(R.id.item_iv_sex, item.getSex() == 1 ? R.mipmap.xb_nan : R.mipmap.xb_nv);
        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
        MainMyReplyListAdapter adapter = new MainMyReplyListAdapter(R.layout.item_reply_publish_comment_layout_my, item.getGsTopicCommentList());
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((a1, view1, position) -> {
            Intent it = new Intent(mContext, SocialStateDetailsAc.class);
            it.putExtra("topicId", adapter.getItem(position).getTopicId());
            it.putExtra("id", adapter.getItem(position).getId());
            mContext.startActivity(it);
        });
        adapter.setOnItemChildClickListener((a, view, position) -> {
            switch (view.getId()) {
                case R.id.item_iv_delete:
                    //删除
                    PublishModel.delGsTopicComment(adapter.getItem(position).getId(), new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            if (!TextUtils.isEmpty(result)) {
                                int index = JSON.parseObject(result).getIntValue("index");
                                ToastUtils.showShort((index == 1) ? "删除成功" : "删除失败");
                                if (index == 1) {
                                    adapter.remove(position);
                                }
                            }
                        }

                        @Override
                        public void onError(String errMsg) {
                            ToastUtils.showShort(errMsg);
                        }
                    });
                    break;
            }
        });
    }
}
